package com.grm.sys.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.grm.common.annotation.HasPerm;
import com.grm.common.annotation.Log;
import com.grm.common.entity.Result;
import com.grm.sys.model.SysUser;
import com.grm.sys.request.user.ChangePasswordParam;
import com.grm.sys.request.user.SysUserAddParam;
import com.grm.sys.request.user.SysUserQueryParam;
import com.grm.sys.request.user.SysUserUpdateParam;
import com.grm.sys.response.SysUserResponse;
import com.grm.sys.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/sys/user")
public class SysUserController {
    @Autowired
    private SysUserService sysUserService;

    /**
     * 查询列表
     */
    @PostMapping("/list")
    public Result list(Integer pageNum, Integer pageSize, @RequestBody SysUserQueryParam queryParam) {
        PageInfo<SysUserResponse> pageInfo = sysUserService.querySelective(pageNum, pageSize, queryParam);
        return Result.success(pageInfo.getList(), pageInfo.getTotal());
    }

    /**
     * 修改密码
     */
    @Log("修改密码")
    @PostMapping("/changePassword")
    public Result changePassword(@RequestBody ChangePasswordParam changePasswordParam) {
        sysUserService.changePassword(changePasswordParam);
        return Result.success("修改密码成功");
    }

    /**
     * 添加用户
     */
    @Log("添加用户")
    @HasPerm("sys:user:add")
    @PostMapping("/add")
    public Result add(@RequestBody SysUserAddParam sysUserAddParam) {
        sysUserService.add(sysUserAddParam);
        return Result.success();
    }

    /**
     * 修改用户
     */
    @Log("修改用户")
    @HasPerm("sys:user:update")
    @PutMapping("/update")
    public Result update(@RequestBody SysUserUpdateParam sysUserUpdateParam) {
        sysUserService.update(sysUserUpdateParam);
        return Result.success();
    }


    /**
     * 删除用户
     */
    @Log("删除用户")
    @HasPerm("sys:user:delete")
    @PostMapping("/delete")
    public Result delete(@RequestBody List<Long> userIds) {
        sysUserService.delete(userIds);
        return Result.success();
    }

    /**
     * 重置密码
     */
    @Log("重置密码")
    @HasPerm("sys:user:resetPassword")
    @GetMapping("/resetPassword/{userId}")
    public Result resetPassword(@PathVariable("userId") Long userId) {
        sysUserService.resetPassword(userId);
        return Result.success();
    }


    /**
     * 获取系统所有用户信息
     */
    @GetMapping("/query/all")
    public Result queryAll() {
        List<SysUserResponse> selectedList = sysUserService.queryAll();
        return Result.success(selectedList);
    }
}
